﻿using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Collections.Generic;

namespace BridgeShips
{
    public class ReceiveUDPData : MonoBehaviour
    {
        private List<string> readList = new List<string>();
        //SpitReceivedMsgs sm = new SpitReceivedMsgs();
        uint id;
        private bool isFirstID;//是否是ID组中的第一个ID
        private Vector3 idsPos;//每个id的坐标
        Socket socket;
        EndPoint clientEnd;
        IPEndPoint ipEnd;
        byte[] recvData = new byte[1024];
        byte[] sendData = new byte[1024];
        int recvLen;
        public string recvStr;
        public string recvOldStr;
        Thread connectThread;


        void Start()
        {
            InitSocket();
            // TestNum();

        }

        void InitSocket()
        {
            ipEnd = new IPEndPoint(IPAddress.Any, 8888);
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.Bind(ipEnd);
            //定义客户端
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            clientEnd = (EndPoint)sender;

            //开启一个线程连接
            connectThread = new Thread(new ThreadStart(SocketReceive));
            connectThread.Start();
        }
        void SocketSend(string sendStr)
        {
            sendData = new byte[1024];
            sendData = Encoding.UTF8.GetBytes(sendStr);
            socket.SendTo(sendData, sendData.Length, SocketFlags.None, clientEnd);
        }
        //服务器接收
        void SocketReceive()
        {
            while (true)
            {
                recvData = new byte[1024];
                recvLen = socket.ReceiveFrom(recvData, ref clientEnd);

                recvStr = Encoding.UTF8.GetString(recvData, 0, recvLen);
                //Debug.LogError("recvStr = " + recvStr);
                SpiteMsg(recvStr);
                //Thread.Sleep(10);
            }
        }
        //连接关闭
        void SocketQuit()
        {
            //关闭线程
            if (connectThread != null)
            {
                connectThread.Interrupt();
                connectThread.Abort();
            }
            //最后关闭socket
            if (socket != null)
                socket.Close();
            Debug.LogWarning("断开连接");
        }
        void OnApplicationQuit()
        {
            SocketQuit();
        }
        private void Update()
        {
            //if (recvStr != null && recvStr != "")
            //{
            //    if (recvStr != recvOldStr)
            //    {
            //        recvOldStr = recvStr;
            //        ReadUDPMsgs(recvStr);
            //    }
            //}
        }
        void SpiteMsg(string str) {
            switch (SpitReceivedMsgs.GetHeadString(str))
            {
                case "river":
                    if (ShipsDictionary.Instance.InitDic.ContainsKey("river"))
                    {
                        ShipsDictionary.Instance.InitDic["river"] = str;
                    }
                    else
                    {
                        ShipsDictionary.Instance.InitDic.Add("river", str);
                    }
                    ShipsDictionary.Instance.isCanUpdateDate = true;
                    break;
                case "AllNum":
                    if (ShipsDictionary.Instance.InitDic.ContainsKey("AllNum"))
                    {
                        ShipsDictionary.Instance.InitDic["AllNum"] = str;
                    }
                    else
                    {
                        ShipsDictionary.Instance.InitDic.Add("AllNum", str);
                    }
                    break;
                case "FourID":
                    //if(str.Contains("非数字"))
                    if (ShipsDictionary.Instance.InitDic.ContainsKey("FourID"))
                    {
                        ShipsDictionary.Instance.InitDic["FourID"] = str;
                    }
                    else
                    {
                        ShipsDictionary.Instance.InitDic.Add("FourID", str);
                    }
                    break;
                case "FiveID":
                    if (ShipsDictionary.Instance.InitDic.ContainsKey("FiveID"))
                    {
                        ShipsDictionary.Instance.InitDic["FiveID"] = str;
                  
                    }
                    else
                    {
                        ShipsDictionary.Instance.InitDic.Add("FiveID", str);
                    }
                    break;
                case "EightID":
                    if (ShipsDictionary.Instance.InitDic.ContainsKey("EightID"))
                    {
                        ShipsDictionary.Instance.InitDic["EightID"] = str;
                    }
                    else
                    {
                        ShipsDictionary.Instance.InitDic.Add("EightID", str);
                    }
                    break;
                case "NineID":
                    if (ShipsDictionary.Instance.InitDic.ContainsKey("NineID"))
                    {
                        ShipsDictionary.Instance.InitDic["NineID"] = str;
                    }
                    else
                    {
                        ShipsDictionary.Instance.InitDic.Add("NineID", str);
                    }
                    break;
                case "TenID":
                    if (ShipsDictionary.Instance.InitDic.ContainsKey("TenID"))
                    {
                        ShipsDictionary.Instance.InitDic["TenID"] = str;
                    }
                    else
                    {
                        ShipsDictionary.Instance.InitDic.Add("TenID", str);
                    }
                    break;
                case "ID": {
                      
                        ShipsDictionary.Instance.AddShipPosition( str );
                       // Debug.LogError("value ==== " + str);
                    }
                    break;
                case "weather":
                    if (ShipsDictionary.Instance.InitDic.ContainsKey("weather"))
                    {
                        ShipsDictionary.Instance.InitDic["weather"] = str;
                    }
                    else
                    {
                        ShipsDictionary.Instance.InitDic.Add("weather", str);
                    }
                    break;
                case "Oposition":
                    if (ShipsDictionary.Instance.InitDic.ContainsKey("Oposition"))
                    {
                        ShipsDictionary.Instance.InitDic["Oposition"] = str;
                    }
                    else
                    {
                        ShipsDictionary.Instance.InitDic.Add("Oposition", str);
                    }
                    break;
                case "Aposition":
                    if (ShipsDictionary.Instance.InitDic.ContainsKey("Aposition"))
                    {
                        ShipsDictionary.Instance.InitDic["Aposition"] = str;
                    }
                    else
                    {
                        ShipsDictionary.Instance.InitDic.Add("Aposition", str);
                    }
                    break;
                //case "Bposition":
                //    if (ShipsDictionary.Instance.InitDic.ContainsKey("Bposition"))
                //    {
                //        ShipsDictionary.Instance.InitDic["Bposition"] = str;
                //    }
                //    else
                //    {
                //        ShipsDictionary.Instance.InitDic.Add("Bposition", str);
                //    }
                //    break;
            }
        }
    }
}
